﻿using System;
using System.Globalization;
using System.Windows.Media;

namespace Songhay.Mvvm.Extensions
{
    /// <summary>
    /// Extensions of <see cref="System.Windows.Media.Color"/>.
    /// </summary>
    public static class ColorExtensions
    {
        /// <summary>
        /// Converts the current value into <see cref="System.Windows.Media.Color"/>.
        /// </summary>
        /// <param name="hexColor">Color of the hex.</param>
        public static Color ToColor(this string hexColor)
        {
            if (string.IsNullOrEmpty(hexColor)) return default(Color);
            uint argb = UInt32.Parse(hexColor.Replace("#", string.Empty), NumberStyles.HexNumber);
            return argb.ToColor();
        }

        /// <summary>
        /// Converts the current value into <see cref="System.Windows.Media.Color"/>.
        /// </summary>
        /// <param name="argb">The ARGB.</param>
        public static Color ToColor(this uint argb)
        {
            return Color.FromArgb(
                (byte)((argb & -16777216) >> 0x18),
                (byte)((argb & 0xff0000) >> 0x10),
                (byte)((argb & 0xff00) >> 8),
                (byte)(argb & 0xff));
        }
    }
}
